package com.baidu.bulls.stock.user.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baidu.bulls.stock.common.exception.BusinessException;
import com.baidu.bulls.stock.common.exception.ComponentException;
import com.baidu.bulls.stock.entity.user.TradeUser;
import com.baidu.bulls.stock.user.service.StockUserService;
import com.baidu.stock.common.web.vo.ApiRespResult;
import com.baidu.stock.common.web.vo.user.TradeUserVo;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@RestController()
@RequestMapping("/user")
@Log4j2
public class StockUserController {

    @Autowired
    private StockUserService stockUserServiceImpl;

    /**
     * 用户登陆接口
     *
     * @param userNo
     * @param userPwd
     * @return
     */
    @RequestMapping("/userLogin")
    public ApiRespResult userLogin(@RequestParam("userNo") String userNo, @RequestParam("userPwd") String userPwd) {

        ApiRespResult result = null;
        try {
            // 用户登陆逻辑处理
            TradeUser tradeUser = stockUserServiceImpl.userLogin(userNo, userPwd);
            result = ApiRespResult.success(tradeUser);
        }  catch (Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }

        return result;

    }

    /**
     * 用户注册接口
     */
    @PostMapping("/register")
    public ApiRespResult<TradeUser> register(@Valid @RequestBody TradeUserVo vo) {
        ApiRespResult<TradeUser> result = null;

        try {
            TradeUser tradeUser = stockUserServiceImpl.userRegister(vo);
            result = ApiRespResult.success(tradeUser);
        } catch(Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }

        return result;
    }

//    @GetMapping("/verification")
//    public void showValidateCodePic(String uuid, HttpServletRequest req, HttpServletResponse res){
//        res.setDateHeader("Expires",0);
//        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
//        res.addHeader("Cache-Control", "post-check=0, pre-check=0");
//        // Set standard HTTP/1.0 no-cache header.
//        res.setHeader("Pragma", "no-cache");
//        res.setContentType("image/jpeg");
//        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);
//        String code = lineCaptcha.getCode();
//        log.debug("uuid={},code={}",uuid,code);
//        try {
//            lineCaptcha.write(res.getOutputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

}